﻿#include "robotinfowid.h"
#include <QTimer>
#include <QMessageBox>
#include "componentmanager.h"


RobotInfoWid::RobotInfoWid(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	ui.label_10->setWordWrap(true);
	ui.label_10->setFixedWidth(250);
	//ui.label_11->setWordWrap(true);
	//ui.label_11->setFixedWidth(250);
	Init();

}

RobotInfoWid::~RobotInfoWid()
{}

void RobotInfoWid::Init()
{
	m_rob = dynamic_cast<Robot*>(component->GetComponent(Component::component_robot));
	if (m_rob == nullptr) {
		ui.label_2->setText("机械手组件获取失败");
		return;
	}

	m_robOpr = new RobotControlDlg(this);
	m_robOpr->Init(m_rob);
	m_robOpr->hide();
	ShowInfo();
}

void RobotInfoWid::ShowInfo()
{
	if (m_rob == nullptr) {
		return;
	}

	auto info = m_rob->GetRobotStateInfo();
	if (info.connected) {
		ui.label_2->setText("连接成功");
	} else {
		ui.label_2->setText("断开连接");
	}
	switch (info.run) {
	case RunState::robot_state_normal:
		ui.label_4->setText("正常");
		break;
	case RunState::robot_state_pause:
		ui.label_4->setText("暂停状态");
		break;
	case RunState::robot_state_stop:
		ui.label_4->setText("停止状态");
		break;
	default:
		ui.label_4->setText("未知状态");
		break;
	}

	ui.label_10->setText(info.target);
	//ui.label_11->setText(info.joints);
	if (info.faultcode == 0) {
		ui.lineEdit_3->setText("");
	} else {
		ui.lineEdit_3->setText(info.faultMsg + QString("(%1)").arg(info.faultcode));
	}
	//SetDIShow(info.diInfo);
	//SetDOShow(info.doinfo);

	ui.label_12->setText(info.diInfo);
	ui.label_13->setText(info.doinfo);
	QTimer::singleShot(1000, [&]() {
		ShowInfo();
		});
}

QString GetIOStyleSheet(const QString& i)
{
	if (i == "1") {
		return "QLabel{min-width:24px;min-height:24px;max-width:24px;max-height:24px;border-radius:8px;border:1px solid black;background:green}";
	} else if (i == "0") {
		return "QLabel{min-width:24px;min-height:24px;max-width:24px;max-height:24px;border-radius:8px;border:1px solid black;background:grey}";
	} else {
		return "QLabel{min-width:24px;min-height:24px;max-width:24px;max-height:24px;border-radius:8px;border:1px solid black;background:red}";
	}
}
void RobotInfoWid::SetDOShow(QString& doi)
{
	//ui.label_Do1->setStyleSheet(GetIOStyleSheet(doi.at(0)));
	//ui.label_Do2->setStyleSheet(GetIOStyleSheet(doi.at(1)));
	//ui.label_Do3->setStyleSheet(GetIOStyleSheet(doi.at(2)));
	//ui.label_Do4->setStyleSheet(GetIOStyleSheet(doi.at(3)));
	//ui.label_Do5->setStyleSheet(GetIOStyleSheet(doi.at(4)));
	//ui.label_Do6->setStyleSheet(GetIOStyleSheet(doi.at(5)));
	//ui.label_Do7->setStyleSheet(GetIOStyleSheet(doi.at(6)));
	//ui.label_Do8->setStyleSheet(GetIOStyleSheet(doi.at(7)));
	//ui.label_Do9->setStyleSheet(GetIOStyleSheet(doi.at(8)));
	//ui.label_Do10->setStyleSheet(GetIOStyleSheet(doi.at(9)));
	//ui.label_Do11->setStyleSheet(GetIOStyleSheet(doi.at(10)));
	//ui.label_Do12->setStyleSheet(GetIOStyleSheet(doi.at(11)));
	//ui.label_Do13->setStyleSheet(GetIOStyleSheet(doi.at(12)));
	//ui.label_Do14->setStyleSheet(GetIOStyleSheet(doi.at(13)));
	//ui.label_Do15->setStyleSheet(GetIOStyleSheet(doi.at(14)));
	//ui.label_Do16->setStyleSheet(GetIOStyleSheet(doi.at(15)));
	//ui.label_Do17->setStyleSheet(GetIOStyleSheet(doi.at(16)));
	//ui.label_Do18->setStyleSheet(GetIOStyleSheet(doi.at(17)));
	//ui.label_Do19->setStyleSheet(GetIOStyleSheet(doi.at(18)));
	//ui.label_Do20->setStyleSheet(GetIOStyleSheet(doi.at(19)));
	//ui.label_Do21->setStyleSheet(GetIOStyleSheet(doi.at(20)));
	//ui.label_Do22->setStyleSheet(GetIOStyleSheet(doi.at(21)));
	//ui.label_Do23->setStyleSheet(GetIOStyleSheet(doi.at(22)));
	//ui.label_Do24->setStyleSheet(GetIOStyleSheet(doi.at(23)));
	//ui.label_Do25->setStyleSheet(GetIOStyleSheet(doi.at(24)));
	//ui.label_Do26->setStyleSheet(GetIOStyleSheet(doi.at(25)));
	//ui.label_Do27->setStyleSheet(GetIOStyleSheet(doi.at(26)));
	//ui.label_Do28->setStyleSheet(GetIOStyleSheet(doi.at(27)));
	//ui.label_Do29->setStyleSheet(GetIOStyleSheet(doi.at(28)));
	//ui.label_Do30->setStyleSheet(GetIOStyleSheet(doi.at(29)));
	//ui.label_Do31->setStyleSheet(GetIOStyleSheet(doi.at(30)));
	//ui.label_Do32->setStyleSheet(GetIOStyleSheet(doi.at(31)));
}
void RobotInfoWid::SetDIShow(QString& di)
{
	//ui.label_Di1->setStyleSheet(GetIOStyleSheet(di.at(0)));
	//ui.label_Di2->setStyleSheet(GetIOStyleSheet(di.at(1)));
	//ui.label_Di3->setStyleSheet(GetIOStyleSheet(di.at(2)));
	//ui.label_Di4->setStyleSheet(GetIOStyleSheet(di.at(3)));
	//ui.label_Di5->setStyleSheet(GetIOStyleSheet(di.at(4)));
	//ui.label_Di6->setStyleSheet(GetIOStyleSheet(di.at(5)));
	//ui.label_Di7->setStyleSheet(GetIOStyleSheet(di.at(6)));
	//ui.label_Di8->setStyleSheet(GetIOStyleSheet(di.at(7)));
	//ui.label_Di9->setStyleSheet(GetIOStyleSheet(di.at(8)));
	//ui.label_Di10->setStyleSheet(GetIOStyleSheet(di.at(9)));
	//ui.label_Di11->setStyleSheet(GetIOStyleSheet(di.at(10)));
	//ui.label_Di12->setStyleSheet(GetIOStyleSheet(di.at(11)));
	//ui.label_Di13->setStyleSheet(GetIOStyleSheet(di.at(12)));
	//ui.label_Di14->setStyleSheet(GetIOStyleSheet(di.at(13)));
	//ui.label_Di15->setStyleSheet(GetIOStyleSheet(di.at(14)));
	//ui.label_Di16->setStyleSheet(GetIOStyleSheet(di.at(15)));
	//ui.label_Di17->setStyleSheet(GetIOStyleSheet(di.at(16)));
	//ui.label_Di18->setStyleSheet(GetIOStyleSheet(di.at(17)));
	//ui.label_Di19->setStyleSheet(GetIOStyleSheet(di.at(18)));
	//ui.label_Di20->setStyleSheet(GetIOStyleSheet(di.at(19)));
	//ui.label_Di21->setStyleSheet(GetIOStyleSheet(di.at(20)));
	//ui.label_Di22->setStyleSheet(GetIOStyleSheet(di.at(21)));
	//ui.label_Di23->setStyleSheet(GetIOStyleSheet(di.at(22)));
	//ui.label_Di24->setStyleSheet(GetIOStyleSheet(di.at(23)));
	//ui.label_Di25->setStyleSheet(GetIOStyleSheet(di.at(24)));
	//ui.label_Di26->setStyleSheet(GetIOStyleSheet(di.at(25)));
	//ui.label_Di27->setStyleSheet(GetIOStyleSheet(di.at(26)));
	//ui.label_Di28->setStyleSheet(GetIOStyleSheet(di.at(27)));
	//ui.label_Di29->setStyleSheet(GetIOStyleSheet(di.at(28)));
	//ui.label_Di30->setStyleSheet(GetIOStyleSheet(di.at(29)));
	//ui.label_Di31->setStyleSheet(GetIOStyleSheet(di.at(30)));
	//ui.label_Di32->setStyleSheet(GetIOStyleSheet(di.at(31)));
}

void RobotInfoWid::on_pushButton_3_clicked()	// 返回原点
{
	auto ans = QMessageBox::question(this, "警告", "执行命令后，机械手会沿直线运动到原点位置，请确认路径范围内无障碍物");
	if (ans == QMessageBox::StandardButton::Yes) {
		auto ret = m_rob->MoveJL("origin");
		if (ret == false) {
			QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
		}
	}
}

void RobotInfoWid::on_pushButton_2_clicked()	// 机械手操作
{
	if (m_robOpr) {
		m_robOpr->show();
	}
}

void RobotInfoWid::on_pushButton_clicked()
{
	QByteArray out;
	if (!m_rob->Reconnecte(out)) {
		QMessageBox::warning(this, "ERROR", "重连失败" + out);
	}
}
